package com.econ.powercloud.ems.controller;

import com.econ.powercloud.common.base.facade.enums.StatusType;
import com.econ.powercloud.common.base.facade.pojo.MsgResult;
import com.econ.powercloud.common.base.facade.pojo.PageResult;
import com.econ.powercloud.ems.constant.SysConstant;
import com.econ.powercloud.ems.pojo.EmsTimingControlVO;
import com.econ.powercloud.ems.pojo.requestVO.EmsTimingControlRequestVO;
import com.econ.powercloud.ems.pojo.requestVO.EmsTimingControlStatusRequestVO;
import com.econ.powercloud.ems.service.biz.TimingControlService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author : zjf
 * @Description : 定时控制controller
 * @Date : 2023/8/15 9:44
 * @Modified by : zjf
 */
@Api(tags ="定时控制")
@RestController
@RequestMapping(value = "/api/v2/timing/control")
@RequiredArgsConstructor
public class TimingControlController extends ApiBaseController{

    private final TimingControlService timingControlService;

    @ApiOperation("分页获取定时控制列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNo", value = "页码", defaultValue = "1", dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "每页大小", defaultValue = "20", dataType = "int")
    })
    @GetMapping("getList")
    public MsgResult<PageResult<List<EmsTimingControlVO>>> getList(@RequestParam(value = "pageNo", defaultValue = SysConstant.DEFAULT_PAGENO) Integer pageNo,
                                                                   @RequestParam(value = "pageSize", defaultValue = SysConstant.DEFAULT_PAGESIZE) Integer pageSize) {
        return successHandler(timingControlService.getList(pageNo,pageSize));
    }

    @ApiOperation("获取某个定时控制的详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "定时控制主键id", dataType = "int"),
    })
    @GetMapping("{id}")
    public MsgResult<EmsTimingControlVO> get(@PathVariable(value = "id")Integer id){
        return successHandler(timingControlService.selectInfoById(id));
    }

    @ApiOperation("删除某个定时控制")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "定时控制主键id", dataType = "int"),
    })
    @DeleteMapping("{id}")
    public MsgResult delete(@PathVariable(value = "id")Integer id){
        timingControlService.deleteTimingControl(id);
        return MsgResult.builder().status(StatusType.OK.getCode()).statusText(StatusType.OK.getStatusText()).data(true).build();
    }

    @ApiOperation("修改某个定时控制的状态")
    @PutMapping("updateStatus")
    public MsgResult updateStatus(@RequestBody @Validated EmsTimingControlStatusRequestVO requestVO){
        timingControlService.updateStatusById(requestVO.getId(),requestVO.getStatus());
        return MsgResult.builder().status(StatusType.OK.getCode()).statusText(StatusType.OK.getStatusText()).data(true).build();
    }

    @ApiOperation("新增单个定时控制")
    @PostMapping()
    public MsgResult updateStatus(@RequestBody @Validated EmsTimingControlRequestVO requestVO){
        timingControlService.insertTimingControl(requestVO);
        return MsgResult.builder().status(StatusType.OK.getCode()).statusText(StatusType.OK.getStatusText()).data(true).build();
    }

    @ApiOperation("修改单个定时控制")
    @PutMapping("{id}")
    public MsgResult updateStatus(@PathVariable(value = "id")Integer id,
                                  @RequestBody @Validated EmsTimingControlRequestVO requestVO){
        timingControlService.updateTimingControl(id,requestVO);
        return MsgResult.builder().status(StatusType.OK.getCode()).statusText(StatusType.OK.getStatusText()).data(true).build();
    }

}
